package com.example.appinting.Fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.appinting.Adapter.myAppointAdapter;
import com.example.appinting.Beans.appoint;
import com.example.appinting.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Appoint extends Fragment {
    private View view;
    private ListView listView;

    private myAppointAdapter adapter;
    private List<HashMap<String, Object>> list;

    private List<appoint> appoints;
    private String patient_id;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        list = new ArrayList<>();

        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey("appointList")) {

            appoints = (List<appoint>) bundle.getSerializable("appointList");
            System.out.println("bundle2不为空");
            //初始化数据
            initDate();
        }
        else {
            System.out.println("bundle2为空");
        }

        System.out.println("准备进入myAppointAdapter");
        adapter = new myAppointAdapter(getActivity(), list);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        patient_id = getActivity().getIntent().getStringExtra("patient_id");
        view = inflater.inflate(R.layout.myappoint, container, false);
        listView = (ListView) view.findViewById(R.id.listview);


////        // 3 填充
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return view;
    }

    private void initDate() {
        try {
//            System.out.println("test11");
            for (appoint appoint :appoints){
//                System.out.println("test12");
                System.out.println(appoint.getPatient_id());
                HashMap<String, Object> map = new HashMap<>();
                map.put("Appoint_id",appoint.getAppoint_id());
                map.put("Doctor_id",appoint.getDoctor_id());
                map.put("Patient_id",appoint.getPatient_id());
                map.put("Doctor_name", appoint.getDoctor_name());
                map.put("Department",appoint.getDepartment());
                map.put("Time",appoint.getStart_time()+"-"+appoint.getEnd_time());
                if (appoint.getAppoint_state() == 0){
                    map.put("State","缴费");
                }
                else {
                    map.put("State","查看");
                }
                list.add(map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
